package com.wenhuitec.android.tang.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wenhuitec.android.tang.R;
import com.wenhuitec.android.tang.eventb.FoodsClickEvent;
import com.wenhuitec.android.tang.ui.adapter.FoodsCardAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class FoodView extends LinearLayout {

    FoodsCardAdapter foodsCardAdapter;


    public FoodView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.view_foods, this, true);

        RecyclerView foodsRv = findViewById(R.id.foodsRv);
        foodsCardAdapter = new FoodsCardAdapter(this.getContext());
        GridLayoutManager layoutManager = new GridLayoutManager(this.getContext(), 5);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        foodsRv.setLayoutManager(layoutManager);
        foodsRv.setAdapter(foodsCardAdapter);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(FoodsClickEvent foodsClickEvent) {
         foodsCardAdapter.notifyDataSetChanged();
    }

    public FoodsCardAdapter getFoodsCardAdapter() {
        return foodsCardAdapter;
    }


    public void setFoodsCardAdapter(FoodsCardAdapter foodsCardAdapter) {
        this.foodsCardAdapter = foodsCardAdapter;
    }
}
